home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / ML_BME1.ZIP / _LIB_ / STRINGS.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-11-14  |  5.0 KB  |  224 lines

  1. Unit Strings;
  2. {
  3.   Strings library v1.0
  4.   by Maple Leaf, 1996
  5.   -------------------------------------------------------------------------
  6.   no comments ...
  7. }
  8.  
  9. interface
  10.  
  11. Function IStr (n : LongInt)  : String;   { int->string }
  12. Function RStr (n : Real)     : String;   { real->string }
  13. Function IVal (s : String)   : LongInt;  { string->int }
  14. Function RVal (s : String)   : Real;     { string->real }
  15. { Fast conversions }
  16.  
  17. Function Dec2Hex (n:longint) : String;   { int->hex(string) }
  18. Function Hex2Dec (s:string)  : LongInt;  { hex(string)->int }
  19. Function Dec2Bin (n:longint) : String;   { int->hex(string) }
  20. Function Bin2Dec (s:string)  : LongInt;  { hex(string)->int }
  21. { basis conversions }
  22.  
  23. function  UCase (txt : string) : string;
  24. { Returns the same text processing letters between 'a' and 'z' to 'A'..'Z' }
  25.  
  26. function  DCase (txt : string) : string;
  27. { Complementary function of UCASE.Transform letters between 'A'-'Z' to 'a'-'z' }
  28.  
  29. function  LTrim(s : String) : string;
  30. function  RTrim(s : String) : string;
  31. function  AllTrim(s : String) : string;
  32. { Functions which provide initial/final/intermediar Space-characters deletion }
  33.  
  34. function DCaseButFirst (txt : string) : string;
  35. { Like DCASE, except the first letter which is gonna be a big one }
  36.  
  37. function  RightPos (str1,str2 : string) : byte;
  38. { Returns position of string STR1 in the string STR2, searching from the
  39.    right to the left position of the string }
  40.  
  41. function  PosOfStr (str1,str2 : string; initpos:byte) : byte;
  42. { Returns the position of STR1 into STR2, starting search with INITPOS position }
  43.  
  44. function  Space (n : byte) : string;
  45. { Returns a string of #32 , with length = n }
  46.  
  47. function  Strng (n:byte; c:byte): string;
  48. { Returns a string which contains n characters with ASCII code C }
  49.  
  50. implementation
  51.  
  52. const HexDigit : string = '0123456789ABCDEF';
  53.       BinDigit : string = '01';
  54.  
  55. Function Dec2Hex (n:longint) : String;   { int->hex(string) }
  56. var s:string; nr:byte;
  57. begin
  58.   s:=''; nr:=0;
  59.   repeat
  60.     s:=HexDigit[1+(n and $F)] + s;
  61.     n:=n shr 4; inc(nr);
  62.   until (n=0) or (n=$FFFFFFFF) or (nr>=8);
  63.   Dec2Hex:=s;
  64. end;
  65.  
  66. Function Hex2Dec (s:string)  : LongInt;  { hex(string)->int }
  67. var n:longint; nr:byte;
  68. begin
  69.   if s='' then begin
  70.     Hex2Dec:=0;
  71.     exit
  72.   end;
  73.   n:=0; nr:=0;
  74.   repeat
  75.     inc(nr);
  76.     n:=(n shl 4) + (pos(s[nr],HexDigit) - 1);
  77.   until (nr>=8) or (nr>=length(s));
  78.   Hex2Dec:=n;
  79. end;
  80.  
  81. Function Dec2Bin (n:longint) : String;   { int->hex(string) }
  82. var s:string; nr:byte;
  83. begin
  84.   s:=''; nr:=0;
  85.   repeat
  86.     s:=BinDigit[1+(n and 1)] + s;
  87.     n:=n shr 1; inc(nr);
  88.   until (n=0) or (n=$FFFFFFFF) or (nr>=32);
  89.   Dec2Bin:=s;
  90. end;
  91.  
  92. Function Bin2Dec (s:string)  : LongInt;  { hex(string)->int }
  93. var n:longint; nr:byte;
  94. begin
  95.   if s='' then begin
  96.     Bin2Dec:=0;
  97.     exit
  98.   end;
  99.   n:=0; nr:=0;
  100.   repeat
  101.     inc(nr);
  102.     n:=(n shl 1) + (pos(s[nr],BinDigit) - 1);
  103.   until (nr>=32) or (nr>=length(s));
  104.   Bin2Dec:=n;
  105. end;
  106.  
  107. Function IStr(n:LongInt) : String;
  108. var qs:string[20];
  109. begin
  110.   str(n,qs); istr:=qs;
  111. end;
  112.  
  113. Function IVal(s:String) : LongInt;
  114. var n:longint; c:integer;
  115. begin
  116.   val(s,n,c); if c<>0 then n:=0;
  117.   ival:=n;
  118. end;
  119.  
  120. Function RStr(n:real) : String;
  121. var qs:string[20];
  122. begin
  123.   str(n:10:4,qs); rstr:=qs;
  124. end;
  125.  
  126. Function RVal(s:String) : real;
  127. var n:longint; c:integer;
  128. begin
  129.   val(s,n,c); if c<>0 then n:=0;
  130.   rval:=n;
  131. end;
  132.  
  133. function  LTrim(s : String) : string;
  134. begin
  135.   while (s[1]=' ') and (length(s)>0) do delete(s,1,1);
  136.   LTrim:=s;
  137. end;
  138.  
  139. function  RTrim(s : String) : string;
  140. begin
  141.   while (s[Length(s)]=' ') and (length(s)>0) do delete(s,Length(s),1);
  142.   RTrim:=s;
  143. end;
  144.  
  145. function  AllTrim(s : String) : string;
  146. var k:byte;
  147. begin
  148.   k:=1;
  149.   while k<=length(s) do begin
  150.     if s[k]=#32 then delete(s,k,1) else inc(k);
  151.   end;
  152.   AllTrim:=s;
  153. end;
  154.  
  155. function ucase;
  156. var n:byte;
  157. begin
  158.   if txt<>'' then for n:=1 to length(txt) do txt[n]:=upcase(txt[n]);
  159.   ucase:=txt;
  160. end;
  161.  
  162. function dcase;
  163. var n:byte;
  164. begin
  165.   if txt<>'' then
  166.     for n:=1 to length(txt) do
  167.       if txt[n] in ['A'..'Z'] then txt[n]:=chr(ord(txt[n])+32);
  168.   dcase:=txt;
  169. end;
  170.  
  171. function dcasebutfirst;
  172. var n:byte;
  173. begin
  174.   if txt<>'' then
  175.     for n:=1 to length(txt) do
  176.       if txt[n] in ['A'..'Z'] then txt[n]:=chr(ord(txt[n])+32);
  177.   n:=1;
  178.   while not(txt[n] in ['a'..'z']) and (n<=length(txt)) do inc(n);
  179.   txt[n]:=upcase(txt[n]);
  180.   dcasebutfirst:=txt;
  181. end;
  182.  
  183.  
  184. function rightpos;
  185. var n,p:byte;
  186. label _1;
  187. begin
  188.   p:=0;
  189.   for n:=length(str2) downto 1 do
  190.     if pos(str1,copy(str2,n,length(str2)-n+1))<>0 then begin
  191.       p:=n-1+pos(str1,copy(str2,n,length(str2)-n+1));
  192.       goto _1;
  193.     end;
  194. _1:
  195.   rightpos:=p;
  196. end;
  197.  
  198. function PosOfStr;
  199. var p:byte;
  200. begin
  201.   p:=initpos-1+pos(str1,copy(str2,initpos,length(str2)-initpos+1));
  202.   if p=initpos-1 then p:=0;
  203.   PosOfStr:=p;
  204. end;
  205.  
  206. function space;
  207. var s:string;
  208. begin
  209.   s:='';
  210.   while length(s)<n do s:=s+' ';
  211.   space:=s;
  212. end;
  213.  
  214. function strng;
  215. var s:string;
  216. begin
  217.   s:='';
  218.   while length(s)<n do s:=s+chr(c);
  219.   strng:=s;
  220. end;
  221.  
  222. begin
  223. end.
  224.